// -*-mode:c++; coding:utf-8-*-

#ifndef _XBASE_GLOBAL_HPP_
#define _XBASE_GLOBAL_HPP_

#include <boost/shared_ptr.hpp>

#include <protocol.hpp>
#include <fs.hpp>
#include <protocol_struct.hpp>

#include "naming_strategy.hpp"

#include "tablet_server_configuration.hpp"
#include "log_manager.hpp"
#include "root_meta_manager.hpp"
#include "tablet.hpp"

namespace xbase
{
	
	class table_schema_manager;
	class table_manager;
	class service_provider;
	class kv_scanner_manager;
	class master_session;

	namespace global
	{
		void init();
		void reset();

		void notify_minor_compact(xbase::tablet::pointer tablet);
		tablet::pointer get_minor_compact_tablet(boost::system_time t);
		void stop_minor_compact();
		
		void notify_major_compact(xbase::tablet::pointer tablet);
		tablet::pointer get_major_compact_tablet(boost::system_time t);
		void stop_major_compact();

		extern boost::shared_ptr<TabletServerConfiguration> the_conf;

		extern boost::shared_ptr<kv_scanner_manager> the_scanner_manager;
		extern boost::shared_ptr<table_schema_manager> the_schema_manager;
		extern boost::shared_ptr<table_manager> the_table_manager;

		extern boost::shared_ptr<protocol::server_address> the_address_info;
		extern boost::shared_ptr<ServerLoad> the_load_info;

		extern boost::shared_ptr<root_meta_manager> the_root_meta_manager;
		extern boost::shared_ptr<log_manager> the_log_manager;

		extern boost::shared_ptr<service_provider> the_service;

		extern boost::shared_ptr<master_session> the_master_session;

		extern fs::path the_tablet_server_path;

		extern bool stop_request;

		//extern tablet_op_manager the_tablet_op_manager;
		//extern root_meta_info_manager the_root_meta_info_manager;

		
		// extern LogThread the_log;


	// 		//
	// // 各种任务线程的定义
	// // 

	// // 日志线程，其中包含了日志队列
	// LogThread *_logThread;

	// // 定期运行，来检查每个子表是否需要主要紧缩或分裂，若需要则对相
	// // 应的子表调用相应的操作。
	// Thread *_splitOrMajorCompactCheckerThread;
	// FastLock _splitOrCompactLock; // 同时只能有一个操作

	// // 定期运行，检查每个子表是否需要次要紧缩，若需要则调用之。
	// Thread *_minorCompactCheckerThread;
	// FastLock _minorCompactLock;	// 同时只能有一个操作

	// LogRollBackThread *_log_roller;

	// TabletLoadingThread *_tabletLoadingThread; // 执行加载子表等耗时的操作


		static inline
		fs::path xbase_root_path() {
			return fs::path(global::the_conf->xbase_root_path);
		}

		static inline
		fs::path server_root_path() {
			return xbase_root_path() / fs::path(naming_strategy::server_dirname(global::the_address_info->m_hostname,
											    global::the_address_info->m_port,
											    global::the_address_info->m_startcode));
		}

	} // namespace global
	
} // namespace xbase

#endif	// _XBASE_GLOBAL_HPP_
